package Nonogram;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JPanel;

/**
 * PanelPuzzleGrid
 * @author Rachel Wedewer,
 * Date Modified: 4/30/2014
 * 
 * An important component of the GUI view.java.  This jpanel class represents the current state of Data's grid.
 * This makes the user able to interact and change the data of the grid that is hidden from them.
 * The grid represents UNKNOWN, BLANK, and FILL states through the color of the cell in the following order:
 * white, yellow, and black. This class also sends the location of the cell that the mouse cursor is currently over to 
 * view.  Layout is a grid layout.
 * 
 */


public class PanelPuzzleGrid extends JPanel{
	
	private static Color UNKNOWN_COLOR = Color.WHITE;
	private static Color BLANK_COLOR = Color.YELLOW;
	private static Color FILL_COLOR = Color.BLACK;
	
	
	private GridArray dataGrid;  //GridArray from Data
	private int gridBoardSize;
	private View viewer;
	private GUICell[][] cellOfBoard;
		
	
	public PanelPuzzleGrid(View view, int boardsize, GridArray gridData){
		this.dataGrid = gridData;
		this.gridBoardSize=boardsize;
		this.viewer = view;
		this.cellOfBoard = new GUICell[this.gridBoardSize][this.gridBoardSize];
		this.setLayout(new GridLayout(gridBoardSize, gridBoardSize,5,5));
		this.setBackground(Color.DARK_GRAY);
		createGirdCells();		

	}
	
	//Left Mouse Click Cycling
    public void clickLeft(MouseEvent e, GUICell cell) {
    	if(cell.getBackground() == (UNKNOWN_COLOR)){
    		cell.setBackground(FILL_COLOR);
    		dataGrid.setCellFillState(cell.getCol(), cell.getRow());
    		updatePreviewPanel();

    		
    	}
    	else if (cell.getBackground() == (FILL_COLOR)){
    		cell.setBackground(BLANK_COLOR);
    		dataGrid.setCellBlankState(cell.getCol(), cell.getRow());
    		updatePreviewPanel();

    	}
    	else if (cell.getBackground() == (BLANK_COLOR)){
    		cell.setBackground(UNKNOWN_COLOR);
    		dataGrid.setCellUnknownState(cell.getCol(), cell.getRow());
    		updatePreviewPanel();

    		}
    }

    //Right Mouse Click Cycling
    public void clickRight(MouseEvent e, GUICell cell) {
    	
    	if(cell.getBackground() == (UNKNOWN_COLOR)){
    		cell.setBackground(BLANK_COLOR);
    		dataGrid.setCellBlankState(cell.getCol(), cell.getRow());
    		updatePreviewPanel();

    	}
    	else if (cell.getBackground() == (BLANK_COLOR)){
    		cell.setBackground(FILL_COLOR);
    		dataGrid.setCellFillState(cell.getCol(), cell.getRow());
    		updatePreviewPanel();

    	}
    	else if (cell.getBackground() == (FILL_COLOR)){
    		cell.setBackground(UNKNOWN_COLOR);
    		dataGrid.setCellUnknownState(cell.getCol(), cell.getRow());
    		updatePreviewPanel();

    		}
    }
	
    //Update the GUI board to reflect changes
    public void updateGUIPuzzle(){
    	this.removeAll();
    	createGirdCells();    	
    }
    
    //Creating the grid
    public void createGirdCells(){
    	//create a 2D gridcell of the GUICells
    	
    	//then adjust them according
    	for(int y=0;y<gridBoardSize; y++){
			for(int x =0; x< gridBoardSize; x++){
				cellOfBoard[x][y] =new GUICell(x,y);  //GUICell is a different class that keeps of its cell location on the grid
				cellOfBoard[x][y].setSize(10, 10);
				if (dataGrid.getCellValue(y,x) == 1){
					cellOfBoard[x][y].setBackground(FILL_COLOR);}
				else if (dataGrid.getCellValue(y,x) == 0){
					cellOfBoard[x][y].setBackground(BLANK_COLOR);}
				else {
					cellOfBoard[x][y].setBackground(UNKNOWN_COLOR);}
				
					final GUICell cell = cellOfBoard[x][y];
					//GUICell is a different class that keeps of its cell location on the grid
					cellOfBoard[x][y].addMouseListener(new MouseListener(){
					
						
					@Override
					public void mouseClicked(MouseEvent e) {
					    
					}

					@Override
					public void mouseEntered(MouseEvent e) {
						//When a GUIcell has the mouse cursor hovering over it
						//It call a method in view with the location of the cell
						//Displays in part of the GUI and is used to highlight
						//the row and column clues that correspond with that cell 
						viewer.setLocationCells(cell.getRow(), cell.getCol());
										
					}

					@Override
					public void mouseExited(MouseEvent e) {
						// TODO Auto-generated method stub
						
					}

					
					@Override
					public void mousePressed(MouseEvent e) {
						// Changing a cell state of a given GUICell
						// Determining the new state by the type of click
						if (e.getModifiers() == MouseEvent.BUTTON3_MASK) { 
							clickRight(e, cell);
							}
						else{clickLeft(e, cell);}
						
					}

					@Override
					public void mouseReleased(MouseEvent e) {
						// TODO Auto-generated method stub
						
					}
		
				});
					
			
			this.add(cellOfBoard[x][y]);
			}
		}
    	
    	updatePreviewPanel();
    }
    
    //Update the PreviewPanel object in View with updated grid data
    private void updatePreviewPanel(){
    	viewer.getPreviewPane().updatePreview();
    	viewer.pack();
    }
    
    
}
